<?php
namespace app\common\model;

use think\Db;

class Weblist extends Base
{
    public static function search($zid)
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();
        $query = $self->alias('a');
        $query->where('a.sup_id', '=', $zid);
        $url = input('post.domain');
        trim($url) && $query->where('a.domain', '=', $url);
        $status = input('post.status');
        is_numeric($status) && $query->where('a.status', '=', $status);
        $qq = input('post.qq');
        is_numeric($qq) && $query->where('a.user_qq', '=', $qq);
        $query2 = clone $query;
        return [
            'total' => $query2->count('a.web_id'),
            'list' => $query->page($page, $pageSize)->order('a.start_time desc')->select()
        ];
    }
	
    public static function delByid($id)
    {
        $self = new static();
        if ($self->where('web_id', '=', $id)->delete()) {
            return true;
        }
        return false;
    }
	
    public static function gjApiUrl()
    {
        switch (config('sys_gjapi')) {
            case 0:
                $gjapiurl = get_domain();
                break;
            case 9:
                $gjapiurl = config('sys_gjapiurl');
                break;
            default;
                $gjapiurl = get_domain();
        }
        return $gjapiurl;
    }

    public static function start_Time()
    {
        $self = new static();
        $start_time = $self
            ->where('web_id', '=', WEB_ID)
            ->field('start_time')
            ->find();
        $sitestart = strtotime($start_time['start_time']);
        $sitenow = time();
        $sitetime = $sitenow - $sitestart;
        $sitedays = (int)($sitetime / 86400);
        return $sitedays;
    }

    public static function end_Time()
    {
        $start = strtotime(config('web_start_time'));
        $end = strtotime(config('web_end_time'));
        $res = ceil(($end - $start) / 86400);
        return $res;
    }

    public static function add_Web($data)
    {
        $validate = validate('Weblist');
        if (!$validate->scene('addfz')->check($data)) {
            $result = [
                'message' => $validate->getError(),
                'code' => 1
            ];
        } else {
            if ($data['domain'] == $_SERVER['HTTP_HOST']) {
                $result = [
                    'message' => '分站域名不能和主站相同',
                    'code' => 1
                ];
            }
            $prefix = get_prefix() . '_';
            $data['prefix'] = $prefix;
            $data['sup_id'] = WEB_ID;
            $data['web_key'] = getRandStr(16);
            $data['start_time'] = date("Y-m-d H:i:s");
            if (!Users::where('uid', '=', $data['user_id'])->field('uid')->find()) {
                $result = [
                    'message' => '该用户ID不存在！',
                    'code' => 1
                ];
            } elseif (Weblist::where('user_id', '=', $data['user_id'])->field('web_id')->find()) {
                $result = [
                    'message' => '该用户已经开通过分站！',
                    'code' => 1
                ];
            } elseif (!check_mail($data['mail'])) {
                $result = [
                    'message' => '站长邮箱格式不正确！',
                    'code' => 1
                ];
            } else {
                $self = new static();
                if ($webid = $self->field('sup_id,user_id,webname,domain,user_qq,mail,start_time,end_time,prefix,web_key')->insertGetId($data)) {
                    Users::where('uid', '=', $data['user_id'])
                        ->update([
                            'power' => 9,
                            'web_id' => $webid
                        ]);
                    $sqls = file_get_contents(ROOT_PATH . '/Other/site.sql');
                    $sqls = str_replace('zy_', $prefix, $sqls);
                    $explode = explode(';', $sqls);
                    foreach ($explode as $sql) {
                        if ($sql = trim($sql)) {
                            Db::query($sql);
                        }
                    }
                    $result = [
                        'message' => '添加分站成功！',
                        'code' => 1
                    ];
                }
            }
        }
        return $result;
    }
	
    public static function updateByWebid($id, $data = [])
    {
        $self = new static();
        return ($self->where('web_id', '=', $id)
                ->update($data) !== false);
    }
}